// pages/result/result.js
const networkManager = require('../../utils/networkManager.js')
const moment = require('../../utils/moment.js')
import Dialog from '../../component/dialog/dialog';

let clicked = false

const compareToToday = (date) => {
  return Number(date.format('YYYYMMDD')) - Number(moment().format('YYYYMMDD'))
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    todayList: [],
    mineList: [],
    tip: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: 'Ticket太票',
    })
  },

  getContent: function() {
    const { token } = wx.getStorageSync('userInfo')

    const promise1 = new Promise((resolve, rej) => {
      networkManager._post_json('/calendar/liveList', { style: [], token }, (res) => {
        resolve()
        if (res && res.code === 0) {
          let list = []
          let todayList = []

          res.data.forEach(item => {
            const date = moment(item.date)
            const compareRzt = compareToToday(date)
            if (compareRzt === 0) {
              todayList = item.list
            }

            list = list.concat(item.list)
            // if (compareRzt > 0) {
            //   list = list.concat(item.list)
            // }
          })

          this.setData({ list, todayList })
        }
      }, (err) => {
        rej(err)
        console.error(err)
      })
    })
    

    const promise2 = new Promise((resolve, rej) => {
      networkManager._get('/mine/performanceBoughtList?token=' + token, (res) => {
        resolve()
        if (res && res.code === 0) {

          res.data && res.data.forEach(item => {
            item.beginTime = moment(item.beginTime).format('YYYY年MM月DD日')
          })

          this.setData({ mineList: res.data })
        }

        if (res && res.code === 1001) {
          this.setData({mineList: []})
        }
      }, (err) => {
        rej(err)
        console.error(err)
      })
    })
    

    return Promise.all([promise1, promise2])
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getContent()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('refresh')
    this.getContent()
      .then(() => wx.stopPullDownRefresh())
      .catch(() => wx.stopPullDownRefresh())
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  getDateString: function(date) {
    return date.format('YYMMDD')
  },

  clickPerformance: function (e) {
    const performanceId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/pay/pay?performanceId=' + performanceId,
    })
  },

  onDelete: function(event) {
    const { position, instance } = event.detail;
    const {orderid, index, status, haspay} = event.currentTarget.dataset

    if (status === 4 || haspay === 0) {
      switch (position) {
        case 'left':
        case 'cell':
        case 'right':
          Dialog.confirm({
            message: '确定删除吗？'
          }).then(() => {

            const { token } = wx.getStorageSync('userInfo')
            networkManager._post_json('/ticket/order/delete', { token, orderid }, (res) => {
              const mineList = this.data.mineList
              mineList.splice(index, 1)

              this.setData({ mineList })
            }, console.error)
            instance.close();
          }).catch(console.error);
          break;
      }
      
    } else {
      return wx.showToast({
        icon: 'none',
        title: '演出未结束 不能删除',
      })
    }

    
  }
})